<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.service.PostService" %>
<%@ page import="com.example.service.UserService" %>
<%@ page import="com.example.service.CommentService" %>
<%@ page import="com.example.service.ReportService" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="java.util.List" %>
<%@ page import="com.example.domain.Post" %>
<%@ page import="com.example.domain.User" %>
<%@ page import="com.example.domain.Comment" %>
<%@ page import="com.example.domain.Report" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>管理员控制台 - 论坛系统</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      background: linear-gradient(135deg, #f8fafc 0%, #e0f2fe 100%);
      min-height: 100vh;
      color: #374151;
    }

    .header {
      background: rgba(255, 255, 255, 0.95);
      backdrop-filter: blur(10px);
      box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
      border-bottom: 1px solid rgba(255, 255, 255, 0.2);
      position: sticky;
      top: 0;
      z-index: 100;
    }

    .header-content {
      max-width: 1200px;
      margin: 0 auto;
      padding: 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-wrap: wrap;
      gap: 16px;
    }

    .header h1 {
      font-size: 28px;
      font-weight: bold;
      background: linear-gradient(135deg, #1f2937 0%, #3b82f6 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
      display: flex;
      align-items: center;
      gap: 12px;
    }

    .logout-btn {
      background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
      color: white;
      padding: 12px 24px;
      border: none;
      border-radius: 25px;
      text-decoration: none;
      font-weight: 600;
      transition: all 0.3s ease;
      display: flex;
      align-items: center;
      gap: 8px;
    }

    .logout-btn:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 25px rgba(239, 68, 68, 0.4);
    }

    .container {
      max-width: 1200px;
      margin: 0 auto;
      padding: 40px 20px;
    }

    .welcome-section {
      background: rgba(255, 255, 255, 0.95);
      backdrop-filter: blur(10px);
      padding: 40px;
      border-radius: 20px;
      box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
      margin-bottom: 40px;
      text-align: center;
      border: 1px solid rgba(255, 255, 255, 0.2);
      animation: fadeInUp 0.6s ease-out;
    }

    .welcome-section h2 {
      font-size: 32px;
      font-weight: bold;
      background: linear-gradient(135deg, #1f2937 0%, #3b82f6 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
      margin-bottom: 12px;
    }

    .welcome-section p {
      color: #6b7280;
      font-size: 18px;
      line-height: 1.6;
    }

    .admin-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
      gap: 30px;
    }

    .admin-card {
      background: rgba(255, 255, 255, 0.95);
      backdrop-filter: blur(10px);
      padding: 40px;
      border-radius: 20px;
      box-shadow: 0 20px 40px rgba(0, 0, 0, 0.08);
      text-align: center;
      transition: all 0.4s ease;
      text-decoration: none;
      color: inherit;
      border: 1px solid rgba(255, 255, 255, 0.2);
      position: relative;
      overflow: hidden;
    }

    .admin-card::before {
      content: '';
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
      transition: left 0.5s ease;
    }

    .admin-card:hover::before {
      left: 100%;
    }

    .admin-card:hover {
      transform: translateY(-10px);
      box-shadow: 0 30px 60px rgba(0, 0, 0, 0.15);
      text-decoration: none;
      color: inherit;
    }

    .card-icon {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      margin: 0 auto 24px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 32px;
      color: white;
      position: relative;
      z-index: 1;
    }

    .card-icon.reports {
      background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
      box-shadow: 0 10px 30px rgba(239, 68, 68, 0.3);
    }

    .card-icon.users {
      background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
      box-shadow: 0 10px 30px rgba(59, 130, 246, 0.3);
    }

    .card-icon.posts {
      background: linear-gradient(135deg, #10b981 0%, #059669 100%);
      box-shadow: 0 10px 30px rgba(16, 185, 129, 0.3);
    }

    .card-icon.comments {
      background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
      box-shadow: 0 10px 30px rgba(245, 158, 11, 0.3);
    }

    .card-title {
      font-size: 24px;
      font-weight: 700;
      color: #1f2937;
      margin-bottom: 12px;
      position: relative;
      z-index: 1;
    }

    .card-description {
      color: #6b7280;
      font-size: 16px;
      line-height: 1.6;
      position: relative;
      z-index: 1;
    }

    .stats-preview {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 16px;
      margin-top: 24px;
      position: relative;
      z-index: 1;
    }

    .stat-item {
      background: rgba(248, 250, 252, 0.8);
      padding: 16px;
      border-radius: 12px;
      text-align: center;
    }

    .stat-number {
      font-size: 20px;
      font-weight: bold;
      color: #1f2937;
      margin-bottom: 4px;
    }

    .stat-label {
      font-size: 12px;
      color: #6b7280;
      text-transform: uppercase;
      letter-spacing: 0.5px;
    }

    @keyframes fadeInUp {
      from {
        opacity: 0;
        transform: translateY(30px);
      }
      to {
        opacity: 1;
        transform: translateY(0);
      }
    }

    .admin-card {
      animation: fadeInUp 0.6s ease-out;
    }

    .admin-card:nth-child(1) { animation-delay: 0.1s; }
    .admin-card:nth-child(2) { animation-delay: 0.2s; }
    .admin-card:nth-child(3) { animation-delay: 0.3s; }
    .admin-card:nth-child(4) { animation-delay: 0.4s; }

    @media (max-width: 768px) {
      .header-content {
        flex-direction: column;
        text-align: center;
      }

      .admin-grid {
        grid-template-columns: 1fr;
        gap: 20px;
      }

      .welcome-section {
        padding: 30px 20px;
      }

      .welcome-section h2 {
        font-size: 28px;
      }

      .admin-card {
        padding: 30px 20px;
      }

      .stats-preview {
        grid-template-columns: 1fr;
        gap: 12px;
      }
    }

    @media (max-width: 480px) {
      .container {
        padding: 20px 16px;
      }

      .welcome-section {
        padding: 24px 16px;
      }

      .admin-card {
        padding: 24px 16px;
      }

      .card-icon {
        width: 60px;
        height: 60px;
        font-size: 24px;
      }
    }
  </style>
</head>
<body>
<%
  // 获取Spring上下文
  WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);

  // 获取各个服务
  PostService postService = context.getBean(PostService.class);
  UserService userService = context.getBean(UserService.class);
  CommentService commentService = context.getBean(CommentService.class);
  ReportService reportService = context.getBean(ReportService.class);

  // 获取统计数据
  List<Post> allPosts = postService.findAllRaw();
  List<User> allUsers = userService.findAll();
  List<Comment> allComments = commentService.findAllForAdmin();
  List<Report> allReports = reportService.findAll();

  // 计算各种统计数据
  int totalPosts = allPosts != null ? allPosts.size() : 0;
  int activePosts = 0;
  int pendingPosts = 0;

  if (allPosts != null) {
    for (Post post : allPosts) {
      if ("active".equals(post.getStatus())) {
        activePosts++;
      }
    }
  }

  int totalUsers = allUsers != null ? allUsers.size() : 0;
  int activeUsers = 0;

  if (allUsers != null) {
    for (User user : allUsers) {
      if ("active".equals(user.getStatus())) {
        activeUsers++;
      }
    }
  }

  int totalComments = allComments != null ? allComments.size() : 0;
  int blockedComments = 0;

  if (allComments != null) {
    for (Comment comment : allComments) {
      if ("blocked".equals(comment.getStatus())) {
        blockedComments++;
      }
    }
  }

  int totalReports = allReports != null ? allReports.size() : 0;
  int pendingReports = 0;
  int processedReports = 0;

  if (allReports != null) {
    for (Report report : allReports) {
      if ("pending".equals(report.getStatus())) {
        pendingReports++;
      } else if ("processed".equals(report.getStatus())) {
        processedReports++;
      }
    }
  }
%>

<div class="header">
  <div class="header-content">
    <h1>
      <span>🛡️</span>
      <span>管理员控制台</span>
    </h1>
    <a href="/admin/logout" class="logout-btn">
      <span>🚪</span>
      <span>退出登录</span>
    </a>
  </div>
</div>

<div class="container">
  <div class="welcome-section">
    <h2>欢迎您，管理员！</h2>
    <p>请选择您要管理的功能模块，维护社区秩序</p>
  </div>

  <div class="admin-grid">
    <a href="/admin/reports" class="admin-card">
      <div class="card-icon reports">🚨</div>
      <div class="card-title">举报审核</div>
      <div class="card-description">查看和处理用户举报的内容，维护社区秩序</div>
      <div class="stats-preview">
        <div class="stat-item">
          <div class="stat-number"><%= pendingReports %></div>
          <div class="stat-label">待处理</div>
        </div>
        <div class="stat-item">
          <div class="stat-number"><%= processedReports %></div>
          <div class="stat-label">已处理</div>
        </div>
      </div>
    </a>

    <a href="/admin/users" class="admin-card">
      <div class="card-icon users">👥</div>
      <div class="card-title">用户管理</div>
      <div class="card-description">管理用户账户，包括激活、封禁等操作</div>
      <div class="stats-preview">
        <div class="stat-item">
          <div class="stat-number"><%= totalUsers %></div>
          <div class="stat-label">总用户</div>
        </div>
        <div class="stat-item">
          <div class="stat-number"><%= activeUsers %></div>
          <div class="stat-label">活跃用户</div>
        </div>
      </div>
    </a>

    <a href="/admin/posts" class="admin-card">
      <div class="card-icon posts">📝</div>
      <div class="card-title">帖子管理</div>
      <div class="card-description">管理论坛帖子，审核和处理不当内容</div>
      <div class="stats-preview">
        <div class="stat-item">
          <div class="stat-number"><%= totalPosts %></div>
          <div class="stat-label">总帖子</div>
        </div>
        <div class="stat-item">
          <div class="stat-number"><%= pendingPosts %></div>
          <div class="stat-label">待审核</div>
        </div>
      </div>
    </a>

    <a href="/admin/comments" class="admin-card">
      <div class="card-icon comments">💬</div>
      <div class="card-title">评论管理</div>
      <div class="card-description">管理用户评论，维护良好的讨论环境</div>
      <div class="stats-preview">
        <div class="stat-item">
          <div class="stat-number"><%= totalComments %></div>
          <div class="stat-label">总评论</div>
        </div>
        <div class="stat-item">
          <div class="stat-number"><%= blockedComments %></div>
          <div class="stat-label">已屏蔽</div>
        </div>
      </div>
    </a>
  </div>
</div>
</body>
</html>
